<?php

/**
 * This is the Widget for create new Support.
 * 
 * @author Tuan Nguyen <nganhtuan63@gmail.com>
 * @version 1.0
 * @package cmswidgets.support
 *
 */
class SlideCreateWidget extends CWidget {

    public $visible = true;

    public function init() {
        
    }

    public function run() {
        if ($this->visible) {
            $this->renderContent();
        }
    }

    protected function renderContent() {
        $model = new Slide;
        $content_resources = Slide::Resources();
        //Work with Resource Binding
        $resource = array();
        $resource_upload = array();
        foreach ($content_resources as $res) {
            $resource_upload[] = GxcHelpers::getArrayResourceObjectBinding('resource_upload_' . $res['type']);
        }
        $test = '';
        $i = 0;
        $count_resource = 0;
        foreach ($content_resources as $cres) {
            $j = 1;
            foreach ($resource_upload[$i] as $res_up) {
                $j++;
                $count_resource++;
            }
            $i++;
        }

        //$model->total_number_resource=$count_resource; 
        //Update Resource Binding Here										
        $i = 0;
        $count_resource = 0;
        foreach ($content_resources as $cres) {
            $j = 1;
            foreach ($resource_upload[$i] as $res_up) {
                //echo $res_up['resid'];
                $resourcenews = Resource::model()->findByPk($res_up['resid']);
                $test = $resourcenews->resource_path;
                $test = str_replace("\\", "/", $test);
                $resourcenews->resource_path = $test;
                $resourcenews->save();
                $j++;
                $count_resource++;
            }
            $i++;
        }


        $is_new = true;
        $process = true;
        $types_array = ConstantDefine::fileTypes();
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'slide-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
        if (isset($_POST['Slide'])) {
            $model->attributes = $_POST['Slide'];
            $model->slide_images = $test;
            if ($model->save()) {
                user()->setFlash('success', t('cms', 'Create new Slide Successfully!'));
                Yii::app()->controller->redirect(array('create'));
            }
        }
        $this->render('cmswidgets.views.slide.slide_form_widget', array(
            'model' => $model,
            'is_new' => $is_new,
            'types_array' => $types_array,
            'slide_id' => ''
        ));
    }

}
